package com.example.house

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.house.adapter.HouseFirstAdapter
import com.example.house.adapter.HouseManngerAdapter
import com.example.house.databinding.ActivityHouseManngerBinding
import com.example.house.entity.HouseEntityItem
import com.example.house.entity.HouseFirstBean
import com.example.house.vm.HouseViewModel
import com.example.house.vm.PinyinUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/house/HouseManngerActivity")
class HouseManngerActivity : BaseMvvmActivity<ActivityHouseManngerBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_house_mannger
    val houseFirstAdapter=HouseFirstAdapter()
    var houseFirstBean= mutableListOf<HouseFirstBean>()
    var name:String=""
    private val houseViewModel: HouseViewModel by  viewModels()
    override fun initView() {
        super.initView()
        mDataBinding.sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    name=query
                    houseViewModel.getListDataAll()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!=null){
                    name=""
                    houseViewModel.getListDataAll()
                }
                return false
            }

        })
        mDataBinding.rv.adapter=houseFirstAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        houseViewModel.getListDataAll()
        houseViewModel.mSuccessLiveData.observe(this){
            houseFirstBean.clear()
            houseFirstAdapter.data.clear()
            val map =it.data.groupBy {
                val pinyin =PinyinUtils.getPinyin(it.enterpriseName)
                PinyinUtils.getFirstLetter(pinyin)
            }
            //对分组的首字母进行排序
            val list=map.keys.sorted()
            for (key in list){
                map[key]?.let {
                    houseFirstBean.add(HouseFirstBean(key,it))
                }
            }
            if (name.isNullOrEmpty()){
                houseFirstAdapter.data.addAll(houseFirstBean)
            }else{
                houseFirstAdapter.data.addAll(houseFirstBean.filter { it.list.any { it.enterpriseName.contains(name) }})
            }
            houseFirstAdapter.notifyDataSetChanged()
        }
        houseViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
    }

}